package com.cell.controller;


import com.cell.common.Result;
import com.cell.common.States;
import com.cell.pojo.DTO.user.validation.validateCodeDTO;
import com.cell.service.user.IUserValidationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cloudJiang
 * @since 2022-06-04
 */
@RestController
@RequestMapping("/userValidation")
@Api(tags = "用户验证码发送以及验证相关接口")
public class UserValidationController {
    @Autowired
    IUserValidationService userValidationService;

    /**
     * 发送验证邮件
     *
     * @return
     */
    @ApiOperation(value = "发送验证邮件", notes = "调用sendValidEmail方法，使用userID参数来发送验证邮件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userID", value = "用户ID", paramType = "int", required = true)
    })
    @GetMapping("/sendValidEmail")
    Result sendValidEmail(@RequestParam("userID") int userID) {
        userValidationService.sendEmail(userID);
        return new Result(null, States.OK.getStates(), States.OK.getDescription());
    }

    /**
     * 发送验证短信
     *
     * @return
     */
    @ApiOperation(value = "发送验证短信", notes = "调用sendValidSms方法，使用userID参数来发送验证邮件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userID", value = "用户ID", paramType = "int", required = true)
    })
    @GetMapping("/sendValidSms")
    Result sendValidSms(@RequestParam("userID") int userID) {
        userValidationService.sendSms(userID);
        return new Result(null, States.OK.getStates(), States.OK.getDescription());
    }

    /**
     * 发送登录验证邮件
     *
     * @return
     */
    @ApiOperation(value = "发送登录验证邮件", notes = "若用户库中无此邮件则抛出异常")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "email", value = "要发送的邮箱", paramType = "String", required = true)
    })
    @GetMapping("/sendValidEmailForLogin")
    Result sendValidEmailForLogin(@RequestParam("email") String email) {
        userValidationService.sendEmailForLogin(email);
        return new Result(null, States.OK.getStates(), States.OK.getDescription());
    }

    /**
     * 发送登录验证短信
     *
     * @return
     */
    @ApiOperation(value = "发送登录验证短信", notes = "若手机号未注册则发送失败")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "发送的手机号", paramType = "String", required = true)
    })
    @GetMapping("/sendValidSmsForLogin")
    Result sendValidSmsForLogin(@RequestParam("phone") String phone) {
        userValidationService.sendSmsForLogin(phone);
        return new Result(null, States.OK.getStates(), States.OK.getDescription());
    }

    /**
     * 验证验证码
     *
     * @return
     */
    @ApiOperation(value = "验证验证码", notes = "调用validateCode方法来发送验证验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loginKey", value = "手机号或邮箱", required = true),
            @ApiImplicitParam(name = "code", value = "验证码", required = true),
            @ApiImplicitParam(name = "validateCodeDTO", value = "以上两个参数封装在此DTO中")
    })
    @PostMapping("/validateCode")
    Result validateCode(@Validated @RequestBody validateCodeDTO validateCodeDTO) {
        userValidationService.validateCode(validateCodeDTO);
        return new Result(null, States.OK.getStates(), States.OK.getDescription());
    }
}

